package com.easonji.attendance.controller;


import com.easonji.admin.service.UserService;
import com.easonji.attendance.model.Vacation;
import com.easonji.attendance.query.VacationQuery;
import com.easonji.attendance.service.IVacationService;
import com.easonji.common.base.controller.BaseController;
import com.easonji.common.base.response.Response;
import com.easonji.common.base.response.enums.ResponseEnum;
import com.easonji.common.constant.enums.message.MessageEnum;
import com.easonji.common.util.StringUtils;
import com.easonji.core.annotion.Permission;
import com.easonji.core.log.LogObjectHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * <p>
 * 假期 前端控制器
 * </p>
 *
 * @author easonji
 * @since 2018-09-23
 */
@Controller
@RequestMapping("/attendance/vacation")
public class VacationController extends BaseController {


    /**
    * 假期页面前缀
    */
    private final static String PREFIX = "attendance/vacation/";

    @Resource
    private IVacationService vacationService;

    @Resource
    private UserService userService;

    /**
    * 跳转到假期首页
    */
    @Permission
    @GetMapping("")
    public String index(Model model) {
        Object result = userService.queryComboList();
        model.addAttribute("userComboList",result);
        return PREFIX + "vacation";
    }

    /**
    * 查询假期列表
    */
    @ResponseBody
    @PostMapping("/list")
    public Object queryList(VacationQuery vacationQuery) {
        Object result = vacationService.queryList(vacationQuery);
        return result;
    }

    /**
    * 跳转到添加假期页面
    */
    @Permission
    @GetMapping("/to_add")
    public String toAdd(Model model) {
        Object result = userService.queryComboList();
        model.addAttribute("userComboList",result);
        return PREFIX + "vacation_edit";
    }

    /**
    * 跳转到修改假期页面
    */
    @Permission
    @GetMapping("/to_update/{vId}")
    public String toUpdate(@PathVariable Integer vId, Model model) {
        Object result = vacationService.queryRowById(vId);
        LogObjectHolder.me().set(result);
        model.addAttribute("vacation", result);
        model.addAttribute("userComboList",userService.queryComboList());
        return PREFIX + "vacation_edit";
    }

    /**
    * 新增假期
    */
    @ResponseBody
    @Permission
    @PostMapping("/save")
    public Response add(@Valid Vacation vacation, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return failure(bindingResult);
        }
        Integer vId = vacationService.queryVidByUserAndYear(vacation.getvUser(),vacation.getvYear());
        if (StringUtils.isNotEmpty(vId)) {
            return failure(MessageEnum.VACAION_EXISTED.getCode(), MessageEnum.VACAION_EXISTED.getMessage());
        }
        int result = vacationService.save(vacation);
        if (result == 0) {
            return failure(ResponseEnum.DATABASE_ERROR);
        }
        return success(ResponseEnum.SAVE_SUCCESS);
    }

    /**
    * 修改假期
    */
    @ResponseBody
    @Permission
    @PostMapping("/update")
    public Response update(@Valid Vacation vacation, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return failure(bindingResult);
        }
        int result = vacationService.update(vacation);
        if (result == 0) {
            return failure(ResponseEnum.DATABASE_ERROR);
        }
        return success(ResponseEnum.UPDATE_SUCCESS);
    }

    /**
    * 删除假期
    */
    @ResponseBody
    @Permission
    @PostMapping("/remove")
    public Response remove(@RequestParam Integer vId) {
        if (StringUtils.isEmpty(vId)) {
            return failure(ResponseEnum.PARAMETER_ERROR);
        }
        int result = vacationService.delete(vId);
        if (result == 0) {
            return failure(ResponseEnum.DATABASE_ERROR);
        }
        return success(ResponseEnum.REMOVE_SUCCESS);
    }

}
